/**  
 * @Title: DownIncomeController.java
 * @Package com.limit.control.admin
 * @author maoyun(maoyun0903@163.com)
 * @date 2014-4-3 上午10:35:33
 * @version V1.0
 */ 
package com.limit.control.admin;

import java.util.Calendar;

import com.limit.constant.Constant;
import com.limit.model.Admin;
import com.limit.model.Game;

/** 
 * <br/>
 * @author maoyun(maoyun0903@163.com)
 * @date 2014-4-3 上午10:35:33 
 * @version V1.0
 */

public class DownIncomeController extends BaseController {
	
	public void index(){
		int type = this.getParaToInt("type", 1);
		// cp的id号
		Long admin_id = this.getSessionAttr(Constant.AdminId);
		System.out.println("管理员主键ID："+admin_id);
		long cp_id 		= this.getParaToLong("cp", 0L);
		//区分登陆角色是超级管理员还是cp   cp是不会传cp_id过来的,只有超级管理员才会传。 所以检测cp_id
		if(cp_id<=0){	
			cp_id = admin_id;
		}
		long   game_id 	= this.getParaToLong("game_id", 0L);
		int    year		= this.getParaToInt("year", 0);
		String month 	= this.getPara("month", "");
		String day 		= this.getPara("day", "");
		
		String temp = new String();
		String series = new String("[##]");
		String apk = new String();
		String ios = new String();
		String nickname = new String();
		String game_name = new String();
		if(game_id > 0L){
			nickname =  Admin.dao.getCacheAdmin(cp_id).getStr("nickname");
			game_name = Game.dao.findById(game_id).getStr("name");
		}
		switch (type){
		case 1:	// 下载统计
			if(year!=0 && !month.equals("") && !day.equals("")){	// 具体到哪天   0-24小时
				//yyyy-MM-dd 
				temp = year + "-" + month + "-" + day + " ";
				String start = "";
				String end  = "";
				for (int i = 0; i < 24; i++) {
					if(i < 10){temp = temp + "0" + i + ":" + "00" + ":" + "00";	
					}else{ temp = temp + i + ":" + "00" + ":" + "00";}
					start = temp;
					temp = temp.substring(0, 11);	// 重置时间节点
					if(i < 10){temp = temp + "0" + i + ":" + "59" + ":" + "59";
					}else{temp = temp + i + ":" + "59" + ":" + "59";}
					end = temp;
					temp = temp.substring(0, 11);
					int num = Game.dao.getListByCreateTime(game_id ,Constant.ANDROID, start, end);
					apk = apk + num + ",";
					num = Game.dao.getListByCreateTime(game_id ,Constant.IOS, start, end);
					ios = ios + num + ",";
				}
				apk = apk.substring(0, apk.length()-1);
				ios = ios.substring(0, ios.length()-1);
				String apk_series = series.replace("##", apk);
//				System.out.println(apk_series);
				this.setAttr("apk_series",apk_series);
				String ios_series = series.replace("##", ios);
//				System.out.println(ios_series);
				this.setAttr("ios_series",ios_series);
				this.setAttr("message",year+"-"+month+"-"+day 
										+ " " + nickname 
										+ " - " + game_name 
										+ " 下载量线性统计图");
				this.setAttr("xAxis",this._day());
				
			}else if(year!=0&&!month.equals("")){		//哪个月
				//计算某年某月有多少天
				Calendar c = Calendar.getInstance();
				c.set(Calendar.YEAR, year);
				c.set(Calendar.MONTH, Integer.valueOf(month));
				int days = c.getActualMaximum(Calendar.DAY_OF_MONTH);
				//yyyy-MM-
				temp = year + "-" + month + "-";
				String start = "";
				String end  = "";
				for (int i = 1; i < days; i++) {
					temp = temp + i + " 00:00:00";
					start = temp;
					temp = temp.substring(0, 11);
					temp = temp + i + " 23:59:59";
					end = temp;
					temp = temp.substring(0, 8);
					int num = Game.dao.getListByCreateTime(game_id ,Constant.ANDROID, start, end);
					apk = apk + num + ",";
					num = Game.dao.getListByCreateTime(game_id ,Constant.IOS, start, end);
					ios = ios + num + ",";
				}
				apk = apk.substring(0, apk.length()-1);
				ios = ios.substring(0, ios.length()-1);
				String apk_series = series.replace("##", apk);
				this.setAttr("apk_series",apk_series);
				String ios_series = series.replace("##", ios);
				this.setAttr("ios_series",ios_series);
				this.setAttr("message",year + "-" + month 
										+ " " + nickname 
										+ " - " + game_name 
										+ " 下载量线性统计图");
				this.setAttr("xAxis",this._month(days));
				
			}else if(year!=0){					//哪年
				//yyyy-MM-dd
				temp = year + "-";
				String start = "";
				String end  = "";
				System.out.println(game_id);
				for (int i = 1; i <= 12; i++) {
					if(i < 10){
						temp = temp + "0" + i + "-01 00:00:00";
					}else{
						temp = temp + i + "-01 00:00:00";
					}
					start = temp;
					temp = temp.substring(0, 5);
					if(i < 10){
						temp = temp + i + "-31 23:59:59";
					}else{
						temp = temp + i + "-31 23:59:59";
					}
					end = temp;
					temp = temp.substring(0, 5);
					int num = Game.dao.getListByCreateTime(game_id ,Constant.ANDROID, start, end);
					apk = apk + num + ",";
					num = Game.dao.getListByCreateTime(game_id ,Constant.IOS, start, end);
					ios = ios + num + ",";
				}
				apk = apk.substring(0, apk.length()-1);
				ios = ios.substring(0, ios.length()-1);
				String apk_series = series.replace("##", apk);
//				System.out.println(apk_series);
				this.setAttr("apk_series",apk_series);
				String ios_series = series.replace("##", ios);
//				System.out.println(ios_series);
				this.setAttr("ios_series",ios_series);
				this.setAttr("message",year 
										+ " " + nickname 
										+ " - " + game_name 
										+ " 下载量线性统计图");
				this.setAttr("xAxis",this._year());
			}else{
				this.setAttr("message","下载量线性统计图");
				this.setAttr("xAxis","[]");
				this.setAttr("apk_series","[]");
				this.setAttr("ios_series","[]");
			}
			break;
		case 2:	//收入统计		暂不作
			
			break;
		}
		Long actor_id = this.getSessionAttr(Constant.ActorId);
		if(actor_id == 1){	//给予超级管理员权限
			this.setAttr("cps", Admin.dao.getListExceptAdmin());
			this.setAttr("isAdmin", true);
		}else{
			this.setAttr("products", Game.dao.getListByAdminId(admin_id));
		}
		this.render("linearChart_down.html");
	}
	
	public void income(){
		
		
	}
	
	private String _year(){
		String string = new String("[");
		for (int i = 1; i <= 12; i++) {
			string = string + "'" + i + "月' ,";
		}
		string = string.substring(0, string.length()-1);
		string += "]";
		return string;
	}
	
	private String _month(int days){
		
		String string = new String("[");
		for (int i = 1; i<=days; i++) {
			string = string + "'" + i + "日' ,";
		}
		string = string.substring(0, string.length()-1);
		string += "]";
		return string;
	}
	
	private String _day(){
		
		String string = new String("[");
		for (int i = 0; i <= 23 ; i++) {
			string = string + "'" + i + "时' ,";
		}
		string = string.substring(0,string.length()-1);
		string += "]";
		return string;
	}
}
